package com.gmrz.uap.dict;


import com.gmrz.uap.common.ServiceSupport;
import com.gmrz.uap.model.Dict;
import com.gmrz.uap.model.DictType;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

public class DictService extends ServiceSupport {

    public static final DictService me = new DictService();

    /**
     * 获取所有数据字典类型
     *
     * @return 所有数据字典类型
     */
    public List<DictType> findDictTypeList() {
        return DictType.dao.findAll();
    }

    /**
     * 根据字典类型ID获取字典列表
     *
     * @param dictTypeId
     * @return
     */
    public List<Dict> getDictListByDictTypeID(String dictTypeId) {
        List<Dict> dictList = Dict.dao.getDictByType(dictTypeId);
        for (int i = 0; i < dictList.size(); i++) {
            Dict dictTmp = dictList.get(i);
            if (!StrKit.isBlank(dictTmp.getDictName())) {
                dictTmp.setDictName(dictTmp.getDictName().trim());
            }

            if (!StrKit.isBlank(dictTmp.getDictText())) {
                dictTmp.setDictText(dictTmp.getDictText().trim());
            }
            dictList.set(i, dictTmp);
        }
        return dictList;
    }

    public List<Dict> getDictList(Dict dict) {
        List<Dict> dictList = Dict.dao.getDictList(dict);
        for (int i = 0; i < dictList.size(); i++) {
            Dict dictTmp = dictList.get(i);
            if (!StrKit.isBlank(dictTmp.getDictName())) {
                dictTmp.setDictName(dictTmp.getDictName().trim());
            }

            if (!StrKit.isBlank(dictTmp.getDictText())) {
                dictTmp.setDictText(dictTmp.getDictText().trim());
            }
            dictList.set(i, dictTmp);
        }
        return dictList;
    }


    public Page<Dict> findListPaginate(Dict dict, int page, int rows, String sort, String order) {
        return Dict.dao.findListPaginate(dict, page, rows, sort, order);
    }


    public boolean exist(String dtId, String dictName) {
        Dict dict = new Dict();
        dict.setDtId(dtId);
        dict.setDictName(dictName);
        return Dict.dao.findOne(dict) == null;
    }


    public Dict findById(String dictId) {
        Dict dict = new Dict();
        dict.setDictId(dictId);
        return Dict.dao.findOne(dict);
    }


    public boolean dictTypeExist(String dtName) {
        return null == DictType.dao.findByName(dtName);
    }


    /**
     * 通过名称查询字典项
     * @param dictName
     * @return
     */
    public Dict findOneByName(String dictName) {
        return Dict.dao.findDictByName(dictName);
    }

}
